package com.briup.jz.service.impl;

import java.util.Date;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.jz.bean.AccountApply;
import com.briup.jz.bean.AccountApplyExample;
import com.briup.jz.bean.extend.AccountApplyExtend;
import com.briup.jz.dao.AccountApplyMapper;
import com.briup.jz.dao.extend.AccountApplyExtendMapper;
import com.briup.jz.service.IAccountApplyService;
import com.briup.jz.utils.CustomerException;
 
@Service
public class AccountApplyServiceImpl implements IAccountApplyService{
	@Autowired
    private AccountApplyMapper accountApplyMapper;
	
	@Autowired
	private AccountApplyExtendMapper accountApplyExtendMapper;
    //查询所有
	@Override
    public List<AccountApply> query(Double money, String apply_type, String status, Long userId) {
    	AccountApplyExample example = new AccountApplyExample();
    	AccountApplyExample.Criteria criteria = example.createCriteria();
    	
    	if(status !=null) {
			criteria.andStatusEqualTo(status);			
		}
		if(apply_type !=null) {
			criteria.andApplyTypeLike("%"+apply_type+"%");
			
		}
		if(money !=null) {
			criteria.andMoneyEqualTo(money);
		}
    	if(userId != null) {
    		criteria.andUserIdEqualTo(userId);
    	}
    	return accountApplyMapper.selectByExample(example);
    }

    //保存或更新信息

    @Override
	public void saveOrupdate(AccountApply accountApply) {
		// TODO Auto-generated method stub
        if (accountApply.getId() == null) {
            // 初始化属性
        	accountApply.setApplyTime(new Date().getTime());
        	accountApply.setStatus("未审核");
            // 插入到数据库
        	accountApplyMapper.insert(accountApply);
        } else {
            accountApplyMapper.updateByPrimaryKey(accountApply);
        }
	}
    //通过ID删除员工
    @Override
    public void deleteById(long id) throws CustomerException {
        AccountApply accountApply = accountApplyMapper.selectByPrimaryKey(id);
        if (accountApply == null) {
            throw new CustomerException("要删除的申请列表不存在");
        } else {
            accountApplyMapper.deleteByPrimaryKey(id);
        }
    }

	@Override
	public List<AccountApplyExtend> queryCascade(Double money, String apply_type, String status, Long userId) {
		// TODO Auto-generated method stub
		return accountApplyExtendMapper.select(money, apply_type, status, userId);
	}

	
}
